#include "httpengine.h"

HttpEngine::HttpEngine()
{
    timerWeb = new QTimer();
    nam = new QNetworkAccessManager();
}

void HttpEngine::start(){
    timerWeb->start(30000);
}

void HttpEngine::stop(){
    timerWeb->stop();
}

void HttpEngine::sendTemperatureToWeb(double temperature, int aireStatus){

    QUrl url;
    url.setHost("sensor-sc.netii.net");
    url.setPath("update.php");
    url.setQueryDelimiters('=', ';');


    url.addQueryItem("temp", QString::number(temperature,'g',6));
    url.addQueryItem("aire", QString::number(aireStatus,10));


    //QUrl url("http://www.sensor-sc.netii.net/update.php?temp=35.4");
    QNetworkReply* reply = nam->get(QNetworkRequest(url));
    // NOTE: Store QNetworkReply pointer (maybe into caller).
    // When this HTTP request is finished you will receive this same
    // QNetworkReply as response parameter.
    // By the QNetworkReply pointer you can identify request and response.
}


void HttpEngine::finishedSlot(QNetworkReply* reply)
{
    // Reading attributes of the reply
    // e.g. the HTTP status code
    QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // Or the target URL if it was a redirect:
    QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    // see CS001432 on how to handle this

    // no error received?
    if (reply->error() == QNetworkReply::NoError)
    {
        // read data from QNetworkReply here

        // Example 1: Creating QImage from the reply
        //QImageReader imageReader(reply);
        //QImage pic = imageReader.read();

        // Example 2: Reading bytes form the reply
        //QByteArray bytes = reply.readAll();  // bytes
        //QString string(bytes); // string
    }
    // Some http error received
    else
    {
        // handle errors here
    }

    // We receive ownership of the reply object
    // and therefore need to handle deletion.
    delete reply;
}
